package cn.jinjimi.app.custom.ui.view;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.jinjimi.base.preference.PreferenceManager;
import cn.jinjimi.base.util.AndroidUtils;
import cn.jinjimi.base.util.CommonUtil;
import cn.jinjimi.base.util.DateUtils;
import cn.jinjimi.base.util.GsonUtils;
import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.bean.BundleKey;
import cn.jinjimi.app.controller.CustomerController;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.entity.CustomTagData;
import cn.jinjimi.app.custom.ui.activity.CustomEditOrAddTagActivity;
import cn.jinjimi.app.custom.ui.activity.CustomInfoActivity;
import cn.jinjimi.app.custom.ui.activity.CustomProposalListActivity;
import cn.jinjimi.app.custom.ui.activity.InsureListActivity;
import cn.jinjimi.domain.data.dto.CustomTagDTO;
import cn.jinjimi.app.store.entity.custom.ContactInfo;
import cn.jinjimi.app.main.dialog.DatePickerDialog;
import cn.jinjimi.app.main.widget.TagView;
import cn.jinjimi.app.bean.SharePrefKeys;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Roy
 * Date: 16/6/3
 */
public class CustomArchivesHeadView extends LinearLayout implements View.OnClickListener {

    private ImageView imgAvatar;
    private TextView tvName;
    private TextView tvGender;
    private TextView tvInfo;
    private TextView tvPhone;
    private TextView tvSorce;
    private TextView tvBirDay;
    private TextView tvInusreCount;
    private TextView tvProposalCount;
    private TextView tvInsureFlag;
    private TextView tvProposalFlag;

    private TextView tvTagTitle;
    private TextView tvTagFlag;
    private TagView tagView;

    private Custom mCustom;

    public CustomArchivesHeadView(Context context) {
        super(context);

        LayoutInflater.from(getContext()).inflate(R.layout.custom_follow_record_header_layout, this);
        initView();
        initListener();
    }

    private void initView() {
        imgAvatar = (ImageView) findViewById(R.id.img_avatar);
        tvName = (TextView) findViewById(R.id.tv_name);
        tvGender = (TextView) findViewById(R.id.tv_gender);
        tvPhone = (TextView) findViewById(R.id.tv_phone);
        tvSorce = (TextView) findViewById(R.id.tv_sorce);
        tvBirDay = (TextView) findViewById(R.id.tv_remind);
        tvInusreCount = (TextView) findViewById(R.id.tv_insure_num);
        tvProposalCount = (TextView) findViewById(R.id.tv_plan_num);
        tvInsureFlag = (TextView) findViewById(R.id.tv_insure_flag);
        tvProposalFlag = (TextView) findViewById(R.id.tv_plan_flag);

        tvInfo = (TextView) findViewById(R.id.tv_info);

        tvTagTitle = (TextView) findViewById(R.id.tv_tag_text);
        tvTagFlag = (TextView) findViewById(R.id.tv_tag_flag);
        tagView = (TagView) findViewById(R.id.tag_view);
    }

    private void initListener() {
        findViewById(R.id.ll_info).setOnClickListener(this);
        findViewById(R.id.ll_tag).setOnClickListener(this);

        findViewById(R.id.ll_plan).setOnClickListener(this);
        findViewById(R.id.ll_insure).setOnClickListener(this);
    }

    /* 设置Custom数据并刷新页面 */
    public void setCustom(Custom custom) {
        this.mCustom = custom;
        onRefresh();
    }

    private void onRefresh() {
        if (mCustom != null) {
            if (StringUtils.isNotEmpty(mCustom.avatar)) {
                imgAvatar.setImageURI(Uri.parse(mCustom.avatar));
            } else {
                imgAvatar.setImageURI(Uri.parse("res:///" + R.drawable.default_bg));
            }

            tvName.setText(mCustom.name);

            //客户性别
            switch (mCustom.gender) {
                case Custom.CUSTOM_GENDER_MAN:
                    tvGender.setText(getContext().getString(R.string.custom_gender_man));
                    tvName.setMaxWidth(AndroidUtils.getWindowWidth(getContext()) - getResources().getDimensionPixelOffset(R.dimen.dp_190));
                    break;
                case Custom.CUSTOM_GENDER_WOMAN:
                    tvGender.setText(getContext().getString(R.string.custom_gender_woman));
                    tvName.setMaxWidth(AndroidUtils.getWindowWidth(getContext()) - getResources().getDimensionPixelOffset(R.dimen.dp_190));
                    break;
                default:
                    tvGender.setText(getContext().getString(R.string.custom_gender_none));
                    tvName.setMaxWidth(AndroidUtils.getWindowWidth(getContext()) - getResources().getDimensionPixelOffset(R.dimen.dp_250));
                    break;
            }

            tvSorce.setText(String.format(getContext().getString(R.string.clue_source), mCustom.sourceName == null ? "" : mCustom.sourceName));
            tvSorce.setVisibility(mCustom.sourceName == null ? GONE : VISIBLE);

            /* 设置保单数据 */
            tvInusreCount.setText(String.format(getContext().getString(R.string.num), mCustom.policy_count));
//            if (mCustom.lastest_policy != null) {
//                tvInsureFlag.setText(mCustom.lastest_policy.getName());
//            }
            tvInsureFlag.setVisibility(GONE);

            /* 设置计划书数据 */
            tvProposalCount.setText(String.format(getContext().getString(R.string.num), mCustom.proposal_count));
            if (mCustom.lastest_proposal != null) {
                tvProposalFlag.setText(mCustom.lastest_proposal.name);
            }

            /* 设置电话号码 */
            if (mCustom.getContactInfos() != null && mCustom.getContactInfos().size() > 0) {
                ContactInfo phone = mCustom.getContactInfos().get(0);
                tvPhone.setText(phone.getValue());
                tvPhone.setVisibility(VISIBLE);
            } else {
                tvPhone.setVisibility(GONE);
            }

            /* 设置标签 */
            setTagText();

            /* 设置生日提醒 */
            setBirDay();
        }
    }

    public void setTagText() {
        if (mCustom == null)
            return;

        CustomTagDTO customTagDTO = GsonUtils.fromJson(PreferenceManager.getStringData(SharePrefKeys.PREF_CUSTOM_TAGS), CustomTagDTO.class);

        if (customTagDTO == null || customTagDTO.getData() == null)
            CustomerController.getCustomTag();
        else if (StringUtils.isNotEmpty(mCustom.label) && !mCustom.label.equals("null")) {
            List<CustomTagData> mShowTagList = new ArrayList<>();

            List<String> list = Arrays.asList(mCustom.label.trim().split(","));//有空格注意
            for (String s1 : list) {
                for (CustomTagData customTagData : customTagDTO.getData()) {
                    if (s1.trim().equals(String.valueOf(customTagData.getId()))) {

                        // 暂时为了解决UI调整后与原有字数限制冲突的UI显示走样的效果~~风险小，改动小，易看懂~
                        if (customTagData.getName().length() > 17)
                            customTagData.setName(customTagData.getName().substring(0, 17) + "..");
                        mShowTagList.add(customTagData);
                        break;
                    }
                }
            }

            if (mShowTagList.size() > 0) {
                tvTagFlag.setVisibility(VISIBLE);
                tvTagTitle.setVisibility(GONE);
                tagView.setVisibility(VISIBLE);

                tagView.setShowTag(mShowTagList);
                return;
            }
        }

        tvTagFlag.setVisibility(INVISIBLE);
        tvTagTitle.setVisibility(VISIBLE);
        tagView.setVisibility(GONE);
    }

    private void setBirDay() {
        if (StringUtils.isEmpty(mCustom.birthday) || mCustom.birthday.equals("null")) {
            tvBirDay.setVisibility(GONE);
            return;
        }
        int difference = mCustom.difference < 0
                ? CommonUtil.getBirthDayCountDown(Long.valueOf(mCustom.birthday))
                : mCustom.difference;

        if (difference > 30) {
            tvBirDay.setVisibility(GONE);
            return;
        }

        tvBirDay.setVisibility(VISIBLE);
        int birtyType = mCustom.birthdayType;

        if (birtyType == DatePickerDialog.OnDateSetListener.TYPE_GREGORIAN_NO_YEAR ||
                birtyType == DatePickerDialog.OnDateSetListener.TYPE_LUNAR_NO_YEAR) {// 无年份生日显示

            if (difference == 0)
                tvBirDay.setText(R.string.custom_today_birthday);
            else
                tvBirDay.setText(String.format(getContext().getString(R.string.custom_birthday_countdown_text), difference));

        } else if (birtyType == DatePickerDialog.OnDateSetListener.TYPE_GREGORIAN_YEAR ||
                birtyType == DatePickerDialog.OnDateSetListener.TYPE_LUNAR_YEAR) {// 有年份生日显示

            if (difference == 0)
                tvBirDay.setText(String.format(getContext().getString(R.string.custom_today_year_birthday), DateUtils.getYearInTimeMillis(mCustom.birthday)));
            else
                tvBirDay.setText(String.format(getContext().getString(R.string.custom_birthday_countdown_year_text), difference, DateUtils.getYearInTimeMillis(mCustom.birthday)));

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_info:     //前往客户资料页面
                openCustomDetail();
                break;
            case R.id.ll_plan:    // 计划书列表
                openCustomProposal();
                break;
            case R.id.ll_insure:  // 保单列表
                openCustomInsure();
                break;
            case R.id.ll_tag:    // 标签页面
                openCustomTag();
                break;
        }
    }

    /* 打开个人资料 */
    @EventTrace(EventID.APP_CUSTOMER_DETAIL_FILE_CLICK)
    private void openCustomDetail() {
        if (mCustom != null) {
            CustomInfoActivity.launch(getContext(), mCustom);
        }
    }

    /* 打开计划书列表 */
    @EventTrace(EventID.APP_CUSTOMER_DETAIL_VPLANCLICK)
    private void openCustomProposal() {
        if (mCustom != null) {
            CustomProposalListActivity.launch(getContext(), mCustom);
        }
    }

    /* 打开保单列表 */
    @EventTrace(EventID.APP_CUSTOMER_DETAIL_POLICY_CLICK)
    private void openCustomInsure() {
        if (mCustom != null) {
            Intent intent = new Intent(getContext(), InsureListActivity.class);
            intent.putExtra(BundleKey.PARAM_CUSTOM, mCustom);
            getContext().startActivity(intent);
        }
    }

    /* 打开标签页面 */
    @EventTrace(EventID.APP_CUSTOMER_DETAIL_TAGCLICK)
    private void openCustomTag() {
        if (mCustom != null) {
            CustomEditOrAddTagActivity.launch(getContext(), mCustom);
        }
    }

    /* 获取个人资料View显示tipsView */
    public View getInfoView() {
        return tvInfo;
    }
}